<%@page import="java.sql.Date"%>
<%@page import="edu.mta.pokerstats.Game"%>
<%@page import="edu.mta.pokerstats.GameRecord"%>
<%@page import="java.util.ArrayList"%>
<%@page import="edu.mta.pokerstats.Player"%>
<%@page import="edu.mta.pokerstats.Team"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	int playerID = Integer.parseInt(request.getParameter("playerID"));
	Player viewedPlayer = Player.getPlayerById(playerID);
	Player viewingPlayer = (Player)session.getAttribute("viewingPlayer");
	if (viewingPlayer.isTeamLeader()){
	%>
		<script type="text/javascript" src="js/editDeleteGameRecords.js"></script>
	<%
	}
%>
<script type="text/javascript">
	$(document).ready(function () {
		$('#PlayerRecordsTable').dataTable({
			"iDisplayLength": 50,
		    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
			"aaSorting": [[0,"desc"]],
			"aoColumnDefs": [
					{ 'bSortable': false, 'aTargets': [7] },
					{ 'bSearchable': false, 'aTargets': [7]}
			]
		});
} );

	function displayGame(i_GameID){
		$.ajax({
			url : "ShowSingleGameRecords.jsp",
			type : "POST",
			data : {
				gameID : i_GameID
			},
			success : function(data, status) {
				changeDivMainContainer(data);
			}
		});
	}
	</script>
<%
	if (viewingPlayer.getTeamId() == viewedPlayer.getTeamId()) {
		ArrayList<GameRecord> gameRecordsList = viewedPlayer.GetGameRecordsForPlayer();
%>
<h3>
	Performance Summary for
	<%=viewedPlayer.getName()%></h3>
<table id="PlayerRecordsTable" class="display">
	<thead>
		<tr>
			<th>Date Of Game</th>
			<th>Number Of Rebuys</th>
			<th>Position</th>
			<th>Knockouts</th>
			<th>Winnings</th>
			<th>Investment</th>
			<th>Balance</th>
			<th>Actions</th>
		</tr>
	</thead>
	<tbody>
	<%
		for (int i = 0; i < gameRecordsList.size(); i++) {
				GameRecord currGR = gameRecordsList.get(i);
				Game currGame = Game.getGameById(currGR.getGameId());
				Date gameDate = currGame.getDate();
				int invested = currGame.GetInvestment(currGR.getNumberOfRebuys());
	%>
		<tr id="row<%=i + 1%>">
			<td><%=gameDate%></td>
			<td id="row<%=i + 1%>Rebuys"><div><%=currGR.getNumberOfRebuys()%></div></td>
			<td id="row<%=i + 1%>Position"><div><%=currGR.getPosition()%></div></td>
			<td id="row<%=i + 1%>Knockouts"><div><%=currGR.getKnockouts()%></div></td>
			<td id="row<%=i + 1%>Winnings"><div><%=currGR.getWonInGame()%></div></td>
			<td id="row<%=i + 1%>Invested"><%=invested%></td>
			<td id="row<%=i + 1%>Balance"><%=currGR.getWonInGame() - invested%></td>
			<td>
		<%
				if (viewingPlayer.isTeamLeader()) {
		%>
				<button id="row<%=i + 1%>EditBtn" onclick="editGameRecord(<%=i + 1%>,<%=currGR.getGameId()%>,<%=currGR.getPlayerId()%>)">Edit</button>
				<button id="row<%=i + 1%>DeleteBtn"	onclick="deleteGameRecord(<%=i + 1%>,<%=currGR.getGameId()%>,<%=currGR.getPlayerId()%>)">Delete</button>
		
				<button id="row<%=i + 1%>SaveBtn" hidden="hidden" onclick="saveEditGameRecord(<%=i + 1%>,<%=currGR.getGameId()%>,<%=currGR.getPlayerId()%>)">Save</button>
				<button id="row<%=i + 1%>CancelBtn" hidden="hidden"	onclick="cancelEditGameRecord(<%=i + 1%>,<%=currGR.getGameId()%>,<%=currGR.getPlayerId()%>)">Cancel</button>
		<%
				}
		%>
				<button onclick="displayGame(<%=currGR.getGameId()%>)">View Game Info</button>
			</td>
		</tr>
	<%
		}
	%>
	</tbody>
</table>
<%
	} else {
%>
<h1>You are not allowed to view information out of your own team!</h1>
<%
	}
%>